package com.lzz.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhenzhen
 * @date 2018-12-18 下午4:25
 */
@Component
@EnableConfigurationProperties(MailProperties.class)
public class JavaMailComponent {
    private static final String template = "mail.html";

    /**
     * mail 发送器类
     */
    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * mail 相关配置类
     */
    @Autowired
    private MailProperties mailProperties;

    /**
     * 模板引擎，用来记载Thymeleaf文件
     */
    @Autowired
    private TemplateEngine templateEngine;

    /**
     * 发送邮件任务对外提供的借口
     * @param email
     */
    public void sendMail(String email) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("email", email);
        map.put("msg", "java email");

        // 加载emil模板
        String text = JavaMailComponent.getTextByTemplate(map, "hello", templateEngine);

        MimeMessage message = this.javaMailSender.createMimeMessage();
        try {
            // 开启带附件
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            InternetAddress from = new InternetAddress();
            from.setAddress(this.mailProperties.getUsername());
            from.setPersonal("李振振", "UTF-8");
            helper.setFrom(from);
            helper.setTo(email);
            helper.setSubject("SpringBoot发送的第一封测试邮件");
            helper.setText(text, true);
            // 添加附件
            FileSystemResource avatar1 = new FileSystemResource(new File("/home/zhenzhen/download/100.jpg"));
            FileSystemResource avatar2 = new FileSystemResource(new File("/home/zhenzhen/download/003.jpg"));
            helper.addAttachment("100.jpg", avatar1);
            helper.addAttachment("003.jpg", avatar2);
            this.javaMailSender.send(message);
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    /***
     * 根据thymeleaf模板渲染出视图，在转化成字符串，返回数据
     * @param dataMap 视图数据
     * @param templateName 模板文件
     * @param templateEngine 模板引擎
     * @return HTML字符串
     */
    private static String getTextByTemplate(Map<String, Object> dataMap, String templateName, TemplateEngine templateEngine) {
        // 要在模板上注入的信息，存入context里面
        Context context = new Context();
        context.setVariables(dataMap);
        String emailText = templateEngine.process(templateName, context);
        return emailText;
    }
}
